package com.kh.first.controller;

import com.kh.first.pojo.common.AjaxJson;
import com.kh.first.pojo.DepartmentPojo;
import com.kh.first.pojo.common.LayTableJson;
import com.kh.first.pojo.common.PageParam;

import com.kh.first.service.DepartmentService;
import com.kh.first.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("departmentController")
public class DepartmentController {
    static final String currentFolder = UserUtils.THEME + "department/";
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(params = "goDepartmentList")
    public String goDepartmentList() {
    return currentFolder + "department-list";
    }


    @RequestMapping(params = "dataGrid")
    @ResponseBody
    public LayTableJson<DepartmentPojo> dataGrid(DepartmentPojo pojo, PageParam pageParam) {
        LayTableJson<DepartmentPojo> ajaxJson = new LayTableJson<>();
        int limit = pageParam.getLimit();
        int page = pageParam.getPage();
        int size = limit == 0 ? 10 : limit;
        pojo.setStart(page == 0 ? 0 : (page - 1) * size);
        pojo.setSize(size);
        List<DepartmentPojo> DepartmentList = departmentService.getDepartmentList(pojo);
        ajaxJson.data = DepartmentList;
        ajaxJson.count = departmentService.countDepartmentList(pojo).intValue();
        return ajaxJson;
    }

    @RequestMapping(params = "goDepartmentAdd")
    public String goDepartmentAdd() {
        return currentFolder + "department-add-update";
    }

    @RequestMapping(params = "goDepartmentQuery")
    public String goDepartmentQuery(Model model,DepartmentPojo pojo){
        model.addAttribute("department", pojo);
        return currentFolder +"department-query";
    }

    @RequestMapping(params = "goDepartmentUpdate")
    public String goDepartmentUpdate(DepartmentPojo department, Model model) {
        List<DepartmentPojo> DepartmentList = departmentService.getDepartmentList(department);
        department = DepartmentList.get(0);//报错让他报
        model.addAttribute("department",department);
        return currentFolder + "department-add-update";
    }

    @RequestMapping(params = "doDepartmentAdd")
    @ResponseBody
    public AjaxJson<String> doUserAdd(DepartmentPojo department) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        departmentService.saveDepartment(department);
        return ajaxJson;
    }
    @RequestMapping(params = "doDepartmentUpdate")
    @ResponseBody
    public AjaxJson<String> doDepartmentUpdate(DepartmentPojo department) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        departmentService.updateDepartmentById(department);
        return ajaxJson;
    }

}
